// pages/confirmorder/confirmorder.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        items: [{
            name: '选择自提',
            value: '1',
            
        },
        {
            name: '送货上门',
            value: '2',
            checked: 'true'
        }
        ],
        multiArray: [
            ['今天', '明天', '后天'],
            ['08:00-10:00', '10:00-12:00', '12:00-14:00', '14:00-16:00', '16:00-18:00']
        ],
        multiIndex: [0,0],
        valuetab: 1,
        currentTab: 0,
        currentBut: 0,
        currentBut1: 0,
        isHide: true, //支付弹窗是否显示
        typePay: "wx_pay",
        addressId: "", //地址id  不必须传 需在 我的 我的地址列表中获取id
        deliveryTime: "",
        buttonClicked: false,
        isphone:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.personalInfo();
        that.addressInfo();
        var totalPrice = options.price * options.num;
        that.setData({
            storeid: options.storeid,
            image: options.image,  
            name: options.name,
            subtitle: options.subtitle,
            price: options.price,
            num: options.num,
            totalPrice: totalPrice.toFixed(2),
            store_goods_id: options.id
        })
    },
    onShow: function () {
        var that = this;
        that.personalInfo();
        that.addressInfo();
    },
    addressInfo: function () {
        var that = this;
        console.log(app.globalData.addressChid);
        if (app.globalData.addressChid != undefined) {
            utils.http("/api/v1/address/address_info", {
                id: app.globalData.addressChid
            }, function (res) {
                app.globalData.addressChid = undefined;
                that.setData({
                    defaultAddress: res.datas,
                    addressId: res.datas.id
                })
            })
        } else {
            utils.http("/api/v1/address/address_info", {}, function (res) {
                console.log(res);
                if (res.datas != null) {
                    that.setData({
                        defaultAddress: res.datas,
                        addressId: res.datas.id
                    })
                }

            })
        }

    },
    //选择自提
    bindMultiPickerChange(e) {
        console.log(e);
        var that = this;
        var arr = e.detail.value;
        var one = that.data.multiArray[0][arr[0]];
        var two = that.data.multiArray[1][arr[1]];
        this.setData({
            multiIndex: e.detail.value,
            deliveryTime: one + "" + two
        })
    },
    //我的个人信息
    personalInfo: function (e) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/member/index", {}, function (res) {
            utils.hideLoading();
            that.setData({
                info: res.datas,
            })
     
        })

    },
    //订单单选项
    radioChange(e) {
        var that = this;
        this.setData({
            valuetab: e.detail.value
        })
        console.log(that.data.info)
        if (e.detail.value == 0) {
            if (that.data.info.phone == "") {
                that.setData({
                    isphone: true
                })
            }
            that.setData({
                addressId: ""
            })
        }
        if (e.detail.value == 1) {
            that.setData({
                addressId: that.data.addressId,
                isphone: false
            })
        }
    },
    //关闭支付弹窗
    onColseTap: function (e) {
        this.setData({
            isHide: true
        })
    },
    //打开支付弹窗
    onOrderTap: function (e) {
        var that = this;
        // 检测时间
        if(that.data.valuetab==1){
        var myDate = new Date();
        var nowH = myDate.getHours();
        var multiIndex = that.data.multiIndex;      //获取当前小时数(0-23)
        if (nowH >= 10 && multiIndex[0] == 0 && multiIndex[1] == 0) {
            wx.showToast({
                title: '请重新选择正确的配送时间',
                icon: "none"
            })
            return false;
        }
        if (nowH >= 12 && multiIndex[0] == 0 && multiIndex[1] <= 1) {
            wx.showToast({
                title: '请重新选择正确的配送时间',
                icon: "none"
            })
            return false;
        }
        if (nowH >= 14 && multiIndex[0] == 0 && multiIndex[1] <= 2) {
            wx.showToast({
                title: '请重新选择正确的配送时间',
                icon: "none"
            })
            return false;
        }
        if (nowH >= 16 && multiIndex[0] == 0 && multiIndex[1] <= 3) {
            wx.showToast({
                title: '请重新选择正确的配送时间',
                icon: "none"
            })
            return false;
        }

        if (nowH >= 18 && multiIndex[0] == 0 && multiIndex[1] <= 4) {
            wx.showToast({
                title: '请重新选择正确的配送时间',
                icon: "none"
            })
            return false;
        }
        }
        this.setData({
            isHide: false
        })
        
    },
    //支付方式
    paymentTypeTap: function (e) {

        var id = e.currentTarget.dataset.id;
        var currentBut = id;
        this.setData({
            currentBut: currentBut,
            typePay: e.currentTarget.dataset.type,
            buttonClicked: false
        })
    },
    //立即支付
    onOrderSuccessTap: function (e) {
        var that = this;
        if (that.data.buttonClicked == false) {
            utils.showLoading();
            that.setData({
                buttonClicked: true
            })
            if (that.data.typePay == "wx_pay") {
                utils.http("/api/v1/order/add_store_order", {
                    store_goods_id: that.data.store_goods_id,
                    num: that.data.num,
                    payment_code: that.data.typePay,
                    get_type: that.data.valuetab,
                    store_id: that.data.storeid,
                    address_id: that.data.addressId,
                    delivery_time: that.data.deliveryTime
                }, function (res) {
                    console.log(res);

                    utils.hideLoading();
                    if (res.state == 1) {
                        wx.requestPayment({
                            timeStamp: res.datas.timeStamp,
                            nonceStr: res.datas.nonceStr,
                            package: res.datas.package,
                            signType: res.datas.signType,
                            paySign: res.datas.paySign,
                            success(res) {
                                wx.redirectTo({
                                    url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
                                })
                            },
                            fail(res) {
                                that.setData({
                                    buttonClicked: false
                                })
                            }
                        })
                    } else {
                        utils.message(res.datas);
                    }
                })
            } else if (that.data.typePay == "balance") {
                utils.http("/api/v1/order/add_store_order", {
                    store_goods_id: that.data.store_goods_id,
                    num:that.data.num,
                    payment_code: that.data.typePay,
                    get_type: that.data.valuetab,
                    store_id: that.data.storeid,
                    address_id: that.data.addressId,
                    delivery_time: that.data.deliveryTime
                }, function (res) {
                    utils.hideLoading()
                    if (res.state == 1) {
                        wx.redirectTo({
                            url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
                        })
                    } else {
                        utils.message(res.datas);
                    }
                })
            }
        }


    },
    //跳入我的地址页面
    onMyAddressTap: function (e) {
        app.globalData.typeaddress = 0
        wx.navigateTo({
            url: '../personal/myaddress/myaddress?typeaddress=' + app.globalData.typeaddress,
        })
    },
    // getPhoneNumber  手机号授权
    getPhoneNumber: function (e) {
        console.log(e);
        var that = this;
        if (e.detail.errMsg == "getPhoneNumber:ok") {
            // 登录
            wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    utils.http("/api/v1/common/get_wechat_phone", {
                        js_code: res.code,
                        iv: e.detail.iv,
                        encryptedData: e.detail.encryptedData
                    }, function (res) {
                        console.log(res);
                        if (res.state == 1) {
                            that.phoneDiel(res.datas);
                            that.setData({
                                isphone: false
                            })
                            wx.showModal({
                                title: '',
                                showCancel: false,
                                confirmText: "授权成功",
                                content: '您已授权手机号成功',
                                success: function (res) {
                                    if (res.confirm) {
                                        that.setData({
                                            isHide: false
                                        })
                                    }

                                }
                            })

                        }
                    })
                }
            })
            // utils.http("/api/v1/member/member_modify", { phone:})

        }


    },
    phoneDiel: function (phone) {
        var that=this;
        utils.http("/api/v1/member/member_modify", {
            phone: phone
        }, function (res) {
            if (res.datas == 1) {
                    that.personalInfo();
            }
        })
    },
})